#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <set>
using namespace std;
#define int long long 
struct cow {
    cow(int x, int y) : _x(x), _id(y) {}
    int _x, _id;
};
vector<cow> a;
set<int> s;
signed main() {
    int N;
    cin >> N;
    for(int i = 0; i < N; i++) {
        int x, y;
        cin >> x >> y;
        s.insert(y);
        a.push_back(cow(x, y));
    }
    int target = s.size();
    sort(a.begin(), a.end(), [](cow& x, cow& y) {
        return x._x < y._x;
    });
    int l = 0, r = 0;
    int len = 0x3f3f3f3f;
    unordered_map<int, int> h;
    while (r < N) {
        h[a[r]._id]++;
        while (h.size() == target) {
            len = min(len, a[r]._x - a[l]._x);
            h[a[l]._id]--;
            if(h[a[l]._id] == 0) {
                h.erase(a[l]._id);
            }
            l++;
        }
        r++;
    }
    cout << len << endl;
    return 0;
}